<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="logo.ico">
    <title>添加轮播图</title>
    <link rel="stylesheet" href="css/eui.css">
</head>
<body>

<div id="app">
    <el-page-header style="background-color: #467097;color: white;line-height: 60px"
            @back="goBack" content="发布轮播图页面"></el-page-header>
    <el-divider></el-divider>
    <el-card>
        <el-form>
            <el-form-item label="轮播图名称" label-width="100px">
                <el-col :span=8>
                <el-input placeholder="请输入内容" v-model="ban.name"></el-input>
                </el-col>
            </el-form-item>
            <el-upload
                    action="/upload"
                    name="picFile"
                    :limit="1"
                    list-type="picture-card"
                    :on-preview="hand"
                    :on-success="handleSuccess"
                    :on-remove="handleRemove">
                <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
            <el-button @click="insert()">发布</el-button>
        </el-form>
    </el-card>
</div>
</body>
<script src="js/vue.js"></script>
<script src="js/eui.js"></script>
<script src="js/axios.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                ban:{
                    name:"",
                    url:""
                }
            }
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
                axios.get("/remove?name="+file.response).then(function (response) {
                    v.$message.success("删除成功");
                })
            },
            hand(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            insert(){
                if (v.ban.url==""){
                    alert("图片不能为空!")
                    return;
                }
                axios.post("/ban/insert",v.ban).then(function (response) {
                        alert("添加完成!");
                        location.href="/admin.html";
                })
            },
            handleSuccess(response,file,fileList){
                v.ban.url = response;
            },
            goBack(){
                history.back();
            }
        }
    })
</script>
</html>